<!--车辆配备 调剂页面-->
<template>
  <div>
    <el-dialog
      :title="doEdit?'操作':'查看'"
      ref="tableDialog"
      :visible.sync="dialogVisible"
      width="80%"
      v-if="formData"
      @close="close"
    >
      <el-tabs v-model="tabIndex">
        <el-tab-pane label="申请单" name="0"></el-tab-pane>
        <el-tab-pane label="选择调剂车辆" name="1" v-if="doEdit"></el-tab-pane>
      </el-tabs>
      <el-form :model="formData" label-width="120px" ref="form" v-show="tabIndex=='0'">
        <el-row>
          <el-col :sm="12">
            <el-form-item label="申请人单位">{{formData.company.companyName}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="使用类型">{{formData.useType}}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :sm="12">
            <el-form-item label="配备方式">{{formData.clname}}</el-form-item>
          </el-col>
          <el-col :sm="12">
            <el-form-item label="车辆类型">{{formData.carType}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="申请原因">{{formData.reason}}</el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="formData.clname=='新购'">
          <el-col :sm="24">
            <el-form-item label="预算指标文件号">{{formData.ysno}}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :sm="24">
            <el-form-item label="附件" style="color:red;">
              <a @click="download(formData.fileurl1,formData.filename1)">{{formData.filename1}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="formData.clname=='新购'">
          <el-col :sm="24">
            <el-form-item label="财政预算指标文件" style="color:red;">
              <a @click="download(formData.fileurl2,formData.filename2)">{{formData.filename2}}</a>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="历史流程">
            <el-table :data="history" style="width: 100%" size="mini" border>
              <el-table-column prop="taskName" label="步骤" align="center"></el-table-column>
              <el-table-column prop="userName" label="审核人" align="center"></el-table-column>
              <el-table-column prop="mobile" label="电话" align="center"></el-table-column>
              <el-table-column prop="mark" label="审批意见" align="center"></el-table-column>
              <el-table-column prop="passStr" label="状态" align="center"></el-table-column>
              <el-table-column prop="optime" label="操作时间" align="center"></el-table-column>
            </el-table>
          </el-form-item>
        </el-row>
      </el-form>
      <carlist ref="carlistComp" v-show="pass"></carlist>
      <el-form label-width="80px">
        <template v-if="doEdit">
          <el-row>
            <el-col :sm="24">
              <el-form-item label="是否通过">
                <el-radio-group v-model="pass">
                  <el-radio :label="1">通过</el-radio>
                  <el-radio :label="0">不通过</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
        <el-row v-if="doEdit&&(type==0||type==1)" style="margin-top: 10px;" v-show="pass">
          <el-col :sm="24">
            <el-form-item label="选择车辆">{{carData?carData.carCode:""}}</el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="doEdit&&(type==0||type==1)" style="margin-top: 10px;">
          <el-col :sm="24">
            <el-form-item label="办理意见">
              <el-input v-model="formData.shenhemark" type="textarea" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" style="text-align: left;">
        <el-button @click="close">关闭</el-button>
        <el-button type="primary" @click="save" v-if="doEdit&&type==0">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { viewJson, sh, dwpbss } from "@/api/task";
import { update } from "@/api/usecar";
import { dateFormat } from "@/utils/utils";
import carlist from "./carlist"; //车辆配备申请如果是调剂类型 那么用此组件选择车辆
export default {
  components: { carlist },
  data() {
    return {
      pass: 1,
      opData: null,
      history: [],
      formData: null,
      doEdit: false,
      dialogVisible: false,
      tabIndex: "0",
      carData: null,
      type: 0, //0待办任务 2历史任务
    };
  },
  methods: {
    close() {
      this.dialogVisible = false;
    },
    /**
     * @description 操作任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    editTask(opData, type) {
      this.viewTask(opData, type);
      this.doEdit = true;
      this.tabIndex = "1";
      this.type = type;
    },
    /**
     * @description 查看任务
     * @param opData 当前操作的任务原始数据 list接口返回的
     * @param type 当前列表的类型 0待办任务 2历史任务 2类型时候，只有调度员可编辑且不能再修改备注了
     */
    async viewTask(opData, type) {
      this.tabIndex = "0";
      this.type = type;
      this.doEdit = false;
      this.opData = opData;
      let params = {
        tableId: opData.tableId,
        pid: opData.processInstanceId,
        taskId: opData.taskId,
        formKey: opData.formKey,
      };
      let r = await viewJson(params);
      this.dialogVisible = true;
      this.formData = r.data;
      this.history.splice(0, this.history.length);
      this.history.push(...r.history);
      //调用子组件获取调剂车辆列表 延迟执行 子组件可能没有初始化好
      setTimeout(() => {
        this.$refs.carlistComp.fetchData();
      }, 0);
      console.log(this.formData);
    },
    //选择调剂的车辆后的回调
    async onCarSelect(carData) {
      this.carData = carData;
    },
    async save() {
      if(this.pass&&!this.carData){
        this.$message({
          message: '请选择车辆',
          type: 'error'
        })
        return
      }
      let params = {
        tableId: this.opData.tableId,
        pid: this.opData.processInstanceId,
        taskId: this.opData.taskId,
        formKey: this.opData.formKey,
        pass: this.pass ? "true" : "false",
        carid: this.carData?this.carData.id:"",
        mark: this.formData.shenhemark,
      };
      let r = await dwpbss(params);
      if (r && r.error == 0) {
        this.finish();
      }
    },
    //各种操作完成后关闭对话框刷新父组件列表
    finish() {
      this.close();
      this.$parent.fetchData();
    },
  },
};
</script>
<style lang="scss" scoped>
.el-timeline {
  position: relative;
  top: -25px;
  left: 80px;
  margin-bottom: -45px;
}
a {
  color: blue;
}
/deep/ .el-col-21 + .el-button {
  display: none;
}
</style>